#include "histogramGray.h"
#include <iostream>

HistogramGray::HistogramGray(): Histogram()
{	
    histogram=new unsigned int[256];
    CDF= new unsigned long[256];

}

HistogramGray::HistogramGray(QImage* mi): Histogram(mi)
{
    histogram=new unsigned int[256];
    CDF= new unsigned long[256];

}
//HistogramGray::HistogramGray(LImage image):Histogram(image.getImage())
//{
    //HistogramGray::HistogramGray(image.getImage());
//}
HistogramGray ::HistogramGray(QImage qImage):Histogram(&qImage)
{
    this->histogram= new unsigned int[256];
    CDF= new unsigned long[256];

}

HistogramGray::~HistogramGray()
{
    //delete []histogram;
}

void HistogramGray::calcHistogram()
{
    if(ownerImage==NULL)
        return;
    else
    {
        for(int i=0;i<256;i++)
        {
            histogram[i]=0;
        }
        for(int x=0;x<ownerImage->width();x++)
            for(int y=0;y<ownerImage->height();y++)
                histogram[qGray(ownerImage->pixel(x,y))]++;
        for(int i=0;i<256;i++)
        {
            std::cout << i << ". " << histogram[i] << std::endl;
        }
        calcHistImage();
    }
}

void HistogramGray::calcHistImage()
{
    if(histImage==NULL)
        histImage=new QImage(256,100,QImage::Format_RGB32);
    unsigned long max=0;
    for(int i=0;i<256;i++)
        if(histogram[i]>max)
            max=histogram[i];
    std::cout << max << std::endl;
    for(int x=0;x<histImage->width();x++)
        for(int y=0;y<histImage->height();y++)
            histImage->setPixel(x,y,qRgb(0,0,0));
    std::cout << "start";
    for(int i=0;i<256;i++)
    {
        histImage->setPixel(i,99-(int)(histogram[i]*99/(double)max),qRgb(255,255,255));
    }
    std::cout << "end";
}
 void HistogramGray::calcCDF()
{
     for(int i=0;i<256;i++)
     {
         CDF[i]=0;
     }
    for(int i=0;i<256;i++)
    {
        for(int j=0;j<=i;j++)
        {
            CDF[i]=histogram[j]+CDF[i];
        }
    }

}
 unsigned long * HistogramGray::getCDF()
 {
     return this->CDF;
 }



